import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiang/pages/home/detail.dart';
import 'package:xiang/pages/house/buildingList.dart';
import 'package:xiang/pages/house/houseDetail.dart';
import 'package:xiang/pages/house/houseForm.dart';
import 'package:xiang/pages/house/houseList.dart';
import 'package:xiang/pages/house/locationList.dart';

import 'package:xiang/pages/login/index.dart';
import 'package:xiang/pages/profile/index.dart';
import 'package:xiang/pages/tab_bar_page.dart';
import 'package:xiang/stores/counter.dart';
import 'package:xiang/utils/tokenManager.dart';

import 'pages/house/roomList.dart';

void main() {
  runApp(MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => CounterModel())],
      child: (MaterialApp(
        routes: {
          '/': (context) => const TabBarPage(),
          '/detail': (context) => const NoticeDetail(),
          '/login': (context) => const LoginPage(),
        },
        //获取用户跳转页面的路径
        onGenerateRoute: (settings) {
          final tokenManager = TokenManager();
          final token = tokenManager.getToken() ?? '';
          //如果没有token而且去的页面不是登录页，则跳转至登录页
          if (token.isEmpty && settings.name != '/login') {
            return MaterialPageRoute(builder: (context) => const LoginPage());
          }

          if (settings.name == '/profile') {
            return MaterialPageRoute(
                builder: (context) => ProfilePage(
                      userInfo: settings.arguments as Map,
                    ));
          }

          if (settings.name == '/houseList') {
            return MaterialPageRoute(builder: (context) => const HouseList());
          }

          if (settings.name == '/locationList') {
            return MaterialPageRoute(
                builder: (context) => const LocationList());
          }

          if (settings.name == '/buildingList') {
            return MaterialPageRoute(
                builder: (context) => BuildingList(
                      point: (settings.arguments as Map)['point'],
                    ));
          }

          if (settings.name == '/roomList') {
            Map temp = settings.arguments as Map;
            return MaterialPageRoute(
                builder: (context) =>
                    RoomList(point: temp['point'], building: temp['building']));
          }

          if (settings.name == '/houseForm') {
            return MaterialPageRoute(
                builder: (context) => HouseForm(
                      houseInfo: settings.arguments as Map,
                    ));
          }

          if (settings.name == '/houseDetail') {
            return MaterialPageRoute(
                builder: (context) =>
                    HouseDetail(house: settings.arguments as Map));
          }

          return MaterialPageRoute(builder: (context) => const TabBarPage());
        },
        initialRoute: '/',
      ))));
}
